package com.lgw.customers.mapper;

import com.lgw.commons.dto.CustomersDTO;
import com.lgw.commons.model.pojo.Customers;
import com.lgw.commons.vo.ShortCustomerInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface CustomersMapper {
    // 根据手机号查询顾客信息
    @Select("select id, username, phone, email, is_valid " +
            " from customers where phone = #{phone}")
    Customers selectByPhone(@Param("phone") String phone);

    // 根据用户名查询顾客信息
    @Select("select id, username, phone, email, is_valid " +
            " from customers where username = #{username}")
    Customers selectByUsername(@Param("username") String username);

    // 新增顾客信息
    @Insert("insert into " +
            " customers (username, password, phone, roles, is_valid, create_date, update_date) " +
            " values (#{username}, #{password}, #{phone}, \"ROLE_USER\", 1, now(), now())")
    int save(CustomersDTO customersDTO);

    // 根据 ID 集合查询多个顾客信息
    @Select("<script> " +
            " select id, nickname, avatar_url from customers " +
            " where is_valid = 1 and id in " +
            " <foreach item=\"id\" collection=\"ids\" open=\"(\" separator=\",\" close=\")\"> " +
            "   #{id} " +
            " </foreach> " +
            " </script>")
    List<ShortCustomerInfo> findByIds(@Param("ids") String[] ids);
}
